package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.ReceiptEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.ReceiptInfoActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReceiptInfoModel {
    private ReceiptInfoActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    private Map map;
    public int total;

    public ReceiptInfoModel(ReceiptInfoActivity activity, SelfRepertory repertory) {
        map = new HashMap();
        this.activity = activity;
        this.repertory = repertory;
    }

    public void receiptList() {
        page = 1;
        map.put("pageNum", page);
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().receiptList(map).enqueue(new Callback<BaseEntity<ListResponse<List<ReceiptEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ReceiptEntity>>>> call, Response<BaseEntity<ListResponse<List<ReceiptEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    total = response.body().getData().getTotal();
                    if (total > 0) {
                        activity.refresh(response.body().getData().getList());
                    } else {
                        activity.empty();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ReceiptEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void lodaMore() {
        page = page + 1;
        map.put("pageNum", 1);
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().receiptList(map).enqueue(new Callback<BaseEntity<ListResponse<List<ReceiptEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ReceiptEntity>>>> call, Response<BaseEntity<ListResponse<List<ReceiptEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    total = response.body().getData().getTotal();
                    activity.loadMore(response.body().getData().getList());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ReceiptEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void receiptDelete(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().receiptDelete(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    activity.deleteSucc();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void receiptUpdate(ReceiptEntity receiptEntity) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().receiptUpdate(receiptEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    receiptList();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }


}
